package com.base.cn.platform.os.controller.agent.income;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.entity.agent.income.AgentIncomeInfo;
import com.base.cn.platform.os.entity.agent.user.AgentUser;
import com.base.cn.platform.os.service.agent.income.AgentIncomeInfoBiz;
import com.base.cn.platform.os.service.agent.user.AgentUserBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 所有推广员收益明细Controller
 *
 * @author LIZHIPENG
 * @create 2018-05-12 下午 12:02
 */
@RestController
@RequestMapping("/course")
public class AgentIncomeInfoController extends BaseController {

    @Autowired
    private AgentIncomeInfoBiz agentIncomeInfoBiz;
    @Autowired
    private AgentUserBiz agentUserBiz;

    /**
     * 查询收益明细列表（分页）
     * @param currentPage 页码
     * @param agentIncomeInfo 条件
     * @param userData 是否设置用户数据（true：是，false：否）
     * @param shopData 是否设置商品数据（true：是，false：否）
     * @return
     */
    @RequestMapping("/queryIncomeInfoListPage")
    public PageInfo<AgentIncomeInfo> queryIncomeInfoListPage(@RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                                             @RequestBody AgentIncomeInfo agentIncomeInfo,
                                                             @RequestParam("userData") boolean userData,
                                                             @RequestParam("shopData") boolean shopData){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        return agentIncomeInfoBiz.queryIncomeInfoListPage(page, agentIncomeInfo, userData, shopData);
    }

    /**
     * 计算可结算总收益
     * @param userId 用户ID
     * @return 可结算总收益金额
     */
    @RequestMapping("/countAvailableIncome")
    public double countAvailableIncome(@RequestParam("userId") BigDecimal userId){
        AgentUser agentUser = agentUserBiz.findAgentUserByUserId(userId, false, false, false);
        Map<String, Object> map = agentIncomeInfoBiz.queryIsAvailableIncome(agentUser);
        double d = 0;
        if(ObjectUtils.isNotEmpty(map)){
            List<AgentIncomeInfo> incomeInfoList = (List<AgentIncomeInfo>)map.get("incomeInfos");
            Double incomePrice = 0D;
            Double bonusPrice = 0D;
            for(AgentIncomeInfo incomeInfo : incomeInfoList){
                if(incomeInfo.getIncomeType().intValue()==2){
                    bonusPrice += incomeInfo.getBonusPrice();
                }
                if(incomeInfo.getIncomeType()==1){
                    incomePrice += incomeInfo.getIncomePrice();
                }
            }
            d = incomePrice + bonusPrice;
        }
        return d;
    }

    /**
     * 保存推广员收益数据列表
     * @param agentIncomeInfoList 收益列表
     */
    @RequestMapping("/saveAgentIncomeInfo")
    public void saveAgentIncomeInfo(@RequestBody List<AgentIncomeInfo> agentIncomeInfoList){
        agentIncomeInfoBiz.saveAgentIncomeInfo(agentIncomeInfoList);
    }
}
